package com.api.workflow.web;

import com.alibaba.fastjson.JSONObject;
import com.api.workflow.util.YNHttpUtils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.util.HashMap;
import java.util.Map;

/**
 * @author wanghan
 * @version 1.0
 * @Description
 * @since
 */
@Path(value = "PRCloseApi")
public class PRClose {
    public static void main(String[] args) {

    }
    @POST
    @Path("/PRClose")
    @Produces(MediaType.TEXT_XML + ";charset=UTF-8")
    public String createORuUpdateClient(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        User user = HrmUserVarify.getUser(request, response);
        User this_user = HrmUserVarify.getUser(request, response);
        int uid = this_user.getUID();
        BaseBean baseBean = new BaseBean();
        baseBean.writeLog("useruseruseruseruseruser"+user.getLastname());
        baseBean.writeLog("PRClose-------start--------");
        Map apiData = new HashMap<>();
        RecordSet rs = new RecordSet();
        try {
            String id = request.getParameter("id");
            baseBean.writeLog("idididid"+id);
            rs.executeQuery("select * from uf_cgsqd where id=?",id);
            rs.next();
            String cgsqbh = Util.null2String(rs.getString("cgsqbh"));
            String cgsqxxm = Util.null2String(rs.getString("cgsqxxm"));
            baseBean.writeLog("cgsqbhcgsqbh"+cgsqbh);
            baseBean.writeLog("cgsqxxmcgsqxxm"+cgsqxxm);
           JSONArray jsonArray1 = new JSONArray();
           JSONObject result = new JSONObject();
            result.put("BANFN", cgsqbh);
            result.put("INSTID", System.currentTimeMillis());
            result.put("BNFPO",cgsqxxm );
            result.put("ZZ001","X");
            jsonArray1.add(result);
            baseBean.writeLog("jsonArray1jsonArray1jsonArray1" + jsonArray1.toString());
            String post = YNHttpUtils.MMI003(jsonArray1.toString(), YNHttpUtils.token());
            baseBean.writeLog("postpostpostpost" + post);
            JSONArray jsonArray = JSONObject.parseArray(post);
            for (int i = 0; i<jsonArray.size();i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                String STATUS = jsonObject.getString("returnstatus");
                String MESSAGE = jsonObject.getString("returnmsg");
                if (!"S".equals(STATUS)) {
                    apiData.put("status","1");
                    apiData.put("data",MESSAGE);
                }else {
                    rs.executeUpdate("update uf_cgsqd set dqzt=3 where id=?",id);
                    apiData.put("status","0");
                    apiData.put("data","");
                }

            }
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("当前接口出现异常"+e.getMessage());
        }

        return JSONObject.toJSONString(apiData);
    }
}
